package com.qianlima.upload.common.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description: 上传文件请求对象
 * @author：allms
 * @date: 2023/3/21 14:43
 */
@Data
@Accessors(chain = true)
@ApiModel("文件DTO")
public class FileDto {
    /**
     * 相对路径
     */
    @ApiModelProperty(value = "文件路径", example = "http://192.168.10.36:9001/file/common/38ad340a-d0db-4e7f-9348-c0890dc95658.crx")
    private String path;

    /**
     * 文件名
     */
    @ApiModelProperty(value = "文件名", example = "我卢本伟没有开挂")
    private String name;

    /**
     * 后缀
     */
    @ApiModelProperty(value = "文件后缀", example = "JPG")
    private String suffix;

    /**
     * 大小|字节B
     */
    @ApiModelProperty(value = "文件大小", example = "1024")
    private Long size;

    @ApiModelProperty(value = "文件索引", example = "2")
    private Integer shardIndex;

    @ApiModelProperty(value = "分片大小", example = "20")
    private Integer shardSize;

    @ApiModelProperty(value = "分片的总大小", example = "1024")
    private Integer shardTotal;
    @ApiModelProperty(value = "文件key", example = "16/32位的md5或16进制的一串乱码")
    private String fileKey;

    /**
     * base64
     */
    @ApiModelProperty(value = "文件Base64")
    private String shard;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "文件大小", example = "1024")
    private Date createTime;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "文件大小", example = "1024")
    private Date updateTime;

    @ApiModelProperty(value = "不存在的分片索引列表")
    private List<Integer> notExistShardIndexList;


    public void verify() throws IllegalArgumentException {
        if (StringUtils.isBlank(this.name)) {
            throw new IllegalArgumentException("文件名不能为空");
        }
        if (StringUtils.isBlank(this.suffix)) {
            throw new IllegalArgumentException("文件后缀名不能为空");
        }
        if (Objects.isNull(this.size) || Objects.isNull(this.shardSize)
                || Objects.isNull(this.shardIndex) || Objects.isNull(this.shardTotal)) {
            throw new IllegalArgumentException("文件大小或分片大小不能为空");
        }
        final int md5Length = 32;
        if (StringUtils.isBlank(this.fileKey) || this.fileKey.length() != md5Length) {
            throw new IllegalArgumentException("文件key校验失败");
        }
        if (this.shardIndex > this.shardTotal) {
            throw new IllegalArgumentException("文件分片校验失败");
        }
    }
}
